<?php
/**
 * 接口日志记录中间件
 * User: zucheng <luzucheng@xin.com>
 * Date: 2018/06/01
 */

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Facades\Log;

class ApiLog
{
    // 不需要记录的路由
    protected $except = [
//        'admin/login',
    ];

    /**
     * Handle an incoming request.
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        if ($request->is(...$this->except)) {
            return $response;
        }

        // 日志记录
        $ret = $request->input();
        if ( $request->path() == 'api/v1/upload') {
            unset($ret['file']);
        }
        $data = [
            'request'  => $ret,
            'response' => $response->original instanceof Arrayable ? $response->original->toArray() : $response->original
        ];
        Log::info($request->path(), $data);

        return $response;
    }
}